//
//  KSDBaseViewController.m
//  KSuodun
//
//  Created by DwZeng on 2022/7/18.
//

#import "KSDBaseViewController.h"

@interface KSDBaseViewController ()
/** 标题的label */
@property (strong ,nonatomic) UILabel *labelTitle;
/** 左边第一个按钮 */
@property (strong ,nonatomic) UIButton *navigationLeftButtonOne;
/** 左边第二个按钮 */
@property (strong ,nonatomic) UIButton *navigationLeftButtonTwo;
/** 右边第一个按钮 */
@property (strong ,nonatomic) UIButton *navigationRightButtonOne;
/** 右边第二个按钮 */
@property (strong ,nonatomic) UIButton *navigationRightButtonTwo;
@end

@implementation KSDBaseViewController

/**
 没有数据点击时候请求接口的回调
 */
- (void)refreshData{
    
    //具体实现交给子控制器
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // 在所有需要隐藏导航栏的页面加上这两行代码，所有需要显示导航栏的页面不做任何操作即可
    self.fd_prefersNavigationBarHidden = YES;
    // 避免出两个导航栏的问题 animated 一定要设置为NO
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

//    self.automaticallyAdjustsScrollViewInsets = NO;

    
    self.view.backgroundColor = [UIColor whiteColor];
    //边缘不留白
    [self createNavigationViewDependDevice];
    
    //在跟控制器上面不添加按钮
    if ([self isKindOfClass:[KSDHomeController class]] || [self isKindOfClass:[KSDShoppingMallController class]] || [self isKindOfClass:[KSDOnlineServiceController class]] || [self isKindOfClass:[KSDMainController class]]){
        self.navigationLeftButton = nil;
    }else{
        KSDBarButtonItem *backButton = [KSDBarButtonItem buttonWithImageNormal:[UIImage imageNamed:@"nav_goback"] imageSelected:[UIImage imageNamed:@"nav_goback"]];
        [backButton addTarget:self  action:@selector(leftItemClick) forControlEvents:UIControlEventTouchUpInside];
        self.navigationLeftButton = backButton;

    }
    

}

//- (UIStatusBarStyle)preferredStatusBarStyle{
//    return UIStatusBarStyleLightContent;
//}



- (void)leftItemClick{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 创建导航栏视图
- (void)createNavigationViewDependDevice{
    //初始化navigationView并添加
    self.navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, navigationHeight)];
    self.navigationView.backgroundColor = [UIColor orangeColor];
    self.line = [[UIImageView alloc] initWithFrame:CGRectMake(0, navigationHeight - 0.4, SCREEN_WIDTH, 0.4)];
    self.line.backgroundColor = [UIColor whiteColor];
    [self.navigationView addSubview:self.line];
    
    self.line.hidden = NO;

    [self.view addSubview:self.navigationView];

}

#pragma mark - 改变导航栏背景透明度
- (void)setNavigationAlpha:(CGFloat)navigationAlpha{
    self.navigationView.backgroundColor = [self.navigationView.backgroundColor colorWithAlphaComponent:navigationAlpha];
}

#pragma mark - 改变导航栏颜色
- (void)setNavigationBackgroundColor:(UIColor *)navigationBackgroundColor{
    self.navigationView.backgroundColor = navigationBackgroundColor;
}

#pragma mark - 设置导航栏标题
- (void)setNavigationTitle:(NSString *)navigationTitle{
    _navigationTitle = navigationTitle;

    UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(67.5, statusHeight, SCREEN_WIDTH - 135, 44)];
    //文字基本设置
    labelTitle.textColor = [UIColor blackColor];

    labelTitle.numberOfLines = 1;
    labelTitle.lineBreakMode = NSLineBreakByTruncatingTail;
    labelTitle.textAlignment = NSTextAlignmentCenter;
    labelTitle.font = [UIFont boldSystemFontOfSize:18.0f];
    labelTitle.text = _navigationTitle;
    if (self.labelTitle) {
        [self.labelTitle removeFromSuperview];
        self.labelTitle = nil;
    }
    self.labelTitle = labelTitle;
    [self.navigationView addSubview:self.labelTitle];
}

#pragma mark - 自定义导航栏标题位置视图
- (void)setNavigationTitleView:(UIControl *)navigationTitleView{
    CGPoint titleView;
    titleView.x = SCREEN_WIDTH / 2;
    titleView.y = self.navigationView.frame.size.height / 2 + 7;
    if (is_iphoneX) {
        titleView.y = 44 + 44 / 2 ;
    }
    navigationTitleView.center = titleView;
    [self.navigationView addSubview:navigationTitleView];
}

#pragma mark - 添加导航栏左侧按钮
- (void)setNavigationLeftButton:(UIButton *)navigationLeftButton{
    //判断是文字按钮还是图片按钮，两者坐标稍有不同
    CGFloat leftButtonWidth = navigationLeftButton.frame.size.width;
    CGFloat leftButtonHeight = navigationLeftButton.frame.size.height;
    
    navigationLeftButton.frame = CGRectMake(10, self.navigationView.frame.size.height / 2 - 8, leftButtonWidth, leftButtonHeight);
    
    if (is_iphoneX) {
        navigationLeftButton.y = 44 + (44 - leftButtonHeight) / 2;
    }
    
    if (_navigationLeftButton) {
        [self.navigationLeftButton removeFromSuperview];
        _navigationLeftButton = nil;
    }
    _navigationLeftButton = navigationLeftButton;
    [self.navigationView addSubview:_navigationLeftButton];
}


#pragma mark - 添加导航栏右侧按钮
- (void)setNavigationRightButton:(UIButton *)navigationRightButton{
    //同上
    CGFloat rightButtonWidth = navigationRightButton.frame.size.width;
    CGFloat rightButtonHeight = navigationRightButton.frame.size.height;
    
    navigationRightButton.frame = CGRectMake(SCREEN_WIDTH - navigationRightButton.frame.size.width - 10, self.navigationView.frame.size.height / 2 - 8, rightButtonWidth, rightButtonHeight);
    if (is_iphoneX) {
        navigationRightButton.y = 44 + (44 - rightButtonHeight) / 2;
    }
    if (_navigationRightButton) {
        [self.navigationRightButton removeFromSuperview];
        _navigationRightButton = nil;
    }
    _navigationRightButton = navigationRightButton;
    [self.navigationView addSubview:_navigationRightButton];
}

#pragma mark - 添加导航栏左侧按钮集合
- (void)setNavigationLeftButtons:(NSArray<UIButton *> *)navigationLeftButtons{
    CGFloat firstLeftButtonWidth = navigationLeftButtons[0].frame.size.width;
    CGFloat firstLeftButtonHeight = navigationLeftButtons[0].frame.size.height;
    
    CGFloat secondLeftButtonWidth = navigationLeftButtons[1].frame.size.width;
    CGFloat secondLeftButtonHeight = navigationLeftButtons[1].frame.size.height;
    
    navigationLeftButtons[0].frame = CGRectMake(10, self.navigationView.frame.size.height / 2 - 8, firstLeftButtonWidth, firstLeftButtonHeight);
    navigationLeftButtons[1].frame = CGRectMake(firstLeftButtonWidth + 10, self.navigationView.frame.size.height / 2 - 8, secondLeftButtonWidth, secondLeftButtonHeight);
    
    if (is_iphoneX) {
        navigationLeftButtons[0].y = 44 + (44 - firstLeftButtonHeight) / 2;
        navigationLeftButtons[1].y  = 44 + (44 - secondLeftButtonHeight) / 2;
    }
    
    //8为两个按钮之间的间隔
    [self.navigationLeftButtonOne removeFromSuperview];
    [self.navigationLeftButtonTwo removeFromSuperview];
    self.navigationLeftButtonOne = nil;
    self.navigationLeftButtonTwo = nil;
    self.navigationLeftButtonOne = navigationLeftButtons[0];
    self.navigationLeftButtonTwo = navigationLeftButtons[1];
    [self.navigationView addSubview:self.navigationLeftButtonOne];
    [self.navigationView addSubview:self.navigationLeftButtonTwo];
}

#pragma mark - 添加导航栏右侧按钮集合
- (void)setNavigationRightButtons:(NSArray<UIButton *> *)navigationRightButtons{
    CGFloat firstRightButtonWidth = navigationRightButtons[0].frame.size.width;
    CGFloat firstRightButtonHeight = navigationRightButtons[0].frame.size.height;
    
    CGFloat secondRightButtonWidth = navigationRightButtons[1].frame.size.width;
    CGFloat secondRightButtonHeight = navigationRightButtons[1].frame.size.height;
    
    navigationRightButtons[0].frame = CGRectMake(SCREEN_WIDTH - 10 - firstRightButtonWidth, self.navigationView.frame.size.height / 2 - 8, firstRightButtonWidth, firstRightButtonHeight);
    navigationRightButtons[1].frame = CGRectMake(SCREEN_WIDTH - 10 - firstRightButtonWidth - 8 - secondRightButtonWidth, self.navigationView.frame.size.height / 2 - 8, secondRightButtonWidth, secondRightButtonHeight);
    
    if (is_iphoneX) {
        
        navigationRightButtons[0].y = 44 + (44 - firstRightButtonHeight) / 2;
        navigationRightButtons[1].y  = 44 + (44 - secondRightButtonHeight) / 2;
    }
    
    //8为两个按钮之间的间隔
    [self.navigationRightButtonOne removeFromSuperview];
    [self.navigationRightButtonTwo removeFromSuperview];
    self.navigationRightButtonOne = nil;
    self.navigationRightButtonTwo = nil;
    self.navigationRightButtonOne = navigationRightButtons[0];
    self.navigationRightButtonTwo = navigationRightButtons[1];
    [self.navigationView addSubview:self.navigationRightButtonOne];
    [self.navigationView addSubview:self.navigationRightButtonTwo];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
